package com.fesine.crm.controller;

import com.fesine.commons.entity.Result;
import com.fesine.commons.util.ResultUtils;
import com.fesine.crm.exception.CrmException;
import com.fesine.crm.po.EventListPo;
import com.fesine.crm.service.EventListService;
import com.fesine.dao.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/23
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/23
 */
@RestController
@RequestMapping("/v1")
public class EventListController {
    @Autowired
    private EventListService service;

    @GetMapping("/events")
    public Result list(EventListPo po) {
        po.addOrderBy(Order.asc("event_date"));
        po.addOrderBy(Order.asc("event_time"));
        po.addOrderBy(Order.asc("name"));
        List<EventListPo> list = service.listAll(po);
        return ResultUtils.success(list);
    }

    /**
     * 分析所有未分析的数据添加到事件列表
     *
     * @return
     */
    @PostMapping("/event/analyse")
    public Result analyseData() {
        try {
            service.analyseData();
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CrmException(404, "没有找到需要分析数据!");
        }
    }

    /**
     * 处理月份异常数据
     * @param monthStr
     * @return
     */
    @PostMapping("/events/error")
    public Result errorDataList(@RequestParam String monthStr,
                            @RequestParam String amTimeStr,
                            @RequestParam String pmTimeStr
                            ) {
        try {
            service.addErrorList(monthStr, amTimeStr, pmTimeStr);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CrmException(404, "没有找到需要分析数据!");
        }
    }

    /**
     * 处理工作日异常数据
     * @param dayStr
     * @return
     */
    @PostMapping("/event/error")
    public Result errorData(@RequestParam String dayStr,
                            @RequestParam String amTimeStr,
                            @RequestParam String pmTimeStr
                            ) {
        try {
            service.addErrorListByDate(dayStr, amTimeStr, pmTimeStr);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CrmException(500, "处理错误!");
        }
    }

}
